//
//  QMCameraPreview.m
//  QMCamera
//
//  Created by guoqingwei on 16/7/20.
//  Copyright © 2016年 cvte. All rights reserved.
//

#import "QMCameraPreview.h"

@interface QMCameraPreview ()

@property (nonatomic, strong) UIImageView *previewImageView;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@end

@implementation QMCameraPreview

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _previewImageView = [[UIImageView alloc] initWithFrame:frame];
        _previewImageView.image = nil;
        [self addSubview:_previewImageView];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self layoutImageView];
}

- (BOOL)inPreview
{
    return !(self.previewImageView.image == nil);
}

- (void)setPreviewImage:(UIImage *)image
{
    if (image) {
        self.backgroundColor = [UIColor blackColor];
        [self clearDetectionEdge];
        [self setNeedsLayout];
    } else {
        self.backgroundColor = [UIColor clearColor];
    }
    
    _previewImageView.image = image;
}

- (void)drawDetectionEdgeWithPoints:(NSArray *)points
{
    [self.shapeLayer removeFromSuperlayer];
    self.shapeLayer = [CAShapeLayer layer];
    self.shapeLayer.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.4].CGColor;
    self.shapeLayer.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.4].CGColor;
    self.shapeLayer.lineWidth = 1.f;
    self.shapeLayer.lineJoin = kCALineJoinRound;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
        
    [path moveToPoint:[[points objectAtIndex:0] CGPointValue]];
    [path addLineToPoint:[[points objectAtIndex:1] CGPointValue]];
    [path addLineToPoint:[[points objectAtIndex:2] CGPointValue]];
    [path addLineToPoint:[[points objectAtIndex:3] CGPointValue]];
    [path closePath];
    
    self.shapeLayer.path = path.CGPath;
    
    [self.layer addSublayer:self.shapeLayer];
}

- (void)clearDetectionEdge
{
    [self.shapeLayer removeFromSuperlayer];
}

#pragma mark - Private methods

- (void)layoutImageView
{
    if (!self.previewImageView.image) {
        self.previewImageView.frame = self.bounds;
        return;
    }
    
    CGSize superSize = self.bounds.size;
    CGSize imageSize = self.previewImageView.image.size;
    
    if (superSize.width >= imageSize.width && superSize.height >= imageSize.height) {
        CGRect frame = CGRectMake((superSize.width - imageSize.width) / 2, (superSize.height - imageSize.height) / 2, imageSize.width, imageSize.height);
        self.previewImageView.frame = frame;
        return;
    }
    
    CGFloat superScale = superSize.width / superSize.height;
    CGFloat imageScale = imageSize.width / imageSize.height;
    
    if (imageScale >= superScale) {
        self.previewImageView.frame =  CGRectMake(0, (superSize.height - superSize.width / imageScale) / 2, superSize.width, superSize.width / imageScale);
    } else {
        self.previewImageView.frame =  CGRectMake((superSize.width - superSize.height * imageScale) / 2, 0, superSize.height * imageScale, superSize.height);
    }
}

@end
